import {
  getStatWorkbenchOverview,
  getStatStatByDay,
  getStatOrderOverview,
  getStatStatByLevel
} from '@/apis/statistics/index.js'
import { typeColors } from '@/utils/config.js'

const orderQuery = JSON.stringify({
  keyword: '', // 关键字
  statusList: '', // 处置状态 "FINISHED"已完成, "PROCESSING"进行中, "TERMINATED"已终止, "CANCELED"已撤销
  infoTypeList: '', // 工单类型 "1"购物,"2"导游,"3"景区,~~~"18"其他
  infoLevelList: '', // 舆情级别 4重大, 1敏感, 2一般, 3重要
  areaCodeList: '', // 所属州市 州市代码
  officeIdList: '', // 所属处室 id
  directUnitIdList: '', // 直属单位 id
  superviseStatusList: '', // 督办反馈状态 0未督办, 1已督办, 2未反馈, 3已反馈
  timeoutStatusList: '', // 办理状态 0正常, 1即将超时, 2已超时
  createTimeStart: '', // 开始时间 2024-10-09 00:00:00
  createTimeEnd: '', // 结束时间 2024-11-08 23:59:59
  nodeStatus: '' // 节点状态 "7"待处置工单, "2"上报待审核, "3"已督办工单, "4"督办待反馈工单, "5"已督办反馈工单
})
export const useOderOverview = () => {
  const homeLoading = ref(false)

  const overviewData = ref({
    total: '0',
    distributing: '0',
    feedbacking: '0',
    distributed: '0',
    handled: '0',
    supervised: '0',
    feedbacked: '0',
    auditing: '0',
    audited: '0'
  })
  const lineData = reactive({
    title: '工单趋势',
    xData: [],
    seriesData: []
  })

  const orderQueryLeader = ref([
    {
      title: '处理中',
      value: '0',
      url: 'WorkOrder',
      query: {
        title: '处理中',
        statusList: 'PROCESSING'
      }
    },
    {
      title: '已超时',
      value: '0',
      url: 'WorkOrder',
      query: {
        title: '已超时',
        timeoutStatusList: 2
      }
    },
    {
      title: '重要敏感信息',
      value: '0',
      url: 'WorkOrder',
      query: {
        title: '重要敏感信息',
        infoLevelList: 3
      }
    },
    {
      title: '重大敏感信息',
      value: '0',
      url: 'WorkOrder',
      query: {
        title: '重大敏感信息',
        infoLevelList: 4
      }
    }
  ])
  const orderQueryStaff = ref([
    {
      title: '已处置',
      value: '0',
      url: '',
      query: {
        ...JSON.parse(orderQuery),
        title: '已处置'
      }
    },
    {
      title: '已审核',
      value: '0',
      url: '',
      query: {
        ...JSON.parse(orderQuery),
        title: '已审核'
      }
    }
  ])
  const orderDisposeStaff = ref([
    {
      title: '待处置',
      value: '0',
      url: 'appToDoPage',
      query: { title: '待处置', type: '1' }
    },
    {
      title: '上报待审核',
      value: '0',
      url: 'appToDoPage',
      query: { title: '上报待审核', type: '2' }
    },
    {
      title: '待督办反馈',
      value: '0',
      url: 'appToDoPage',
      query: { title: '待督办反馈', type: '3' }
    },
    {
      title: '已督办反馈',
      value: '0',
      url: 'SuperviseFeedback',
      query: { title: '已督办反馈', nodeStatus: '5' }
    }
  ])

  const totalData = ref([])
  const totalSum = computed(() => {
    return totalData.value.reduce((sum, item) => sum + item?.total, 0)
  })

  const calculatePercentage = (total, totalSum) => {
    if (!total || !totalSum) return '0%' // 防止percentage出现NaN的情况
    let percentage = ((total / totalSum) * 100).toFixed(2)
    return percentage === '0.00' ? '0%' : percentage + '%'
  }

  const typeRatioList = computed(() => {
    return [
      {
        label: '重大',
        color: typeColors.infoSensitiveMajor,
        value: calculatePercentage(
          totalData.value.find((item) => item?.label === '重大敏感信息')
            ?.total || 0,
          totalSum.value
        )
      },
      {
        label: '重要',
        color: typeColors.infoSensitiveImportant,
        value: calculatePercentage(
          totalData.value.find((item) => item?.label === '重要敏感信息')
            ?.total || 0,
          totalSum.value
        )
      },
      {
        label: '敏感',
        color: typeColors.infoSensitiveGeneral,
        value: calculatePercentage(
          totalData.value.find((item) => item?.label === '敏感信息')?.total ||
            0,
          totalSum.value
        )
      },
      {
        label: '一般',
        color: typeColors.infoSensitive,
        value: calculatePercentage(
          totalData.value.find((item) => item?.label === '一般敏感信息')
            ?.total || 0,
          totalSum.value
        )
      }
    ]
  })
  // 获取统计总览数据
  async function getHomeTotalData(query) {
    try {
      const res = await getStatStatByLevel(query)
      if (res) {
        let { ret, data } = res
        if (ret === 0) {
          totalData.value = data || []
        }
      }
    } catch (error) {
      console.error(error)
    }
  }
  // 获取统计总览数据-执行版
  async function getHomeOverviewDataStaff(query) {
    try {
      const res = await getStatWorkbenchOverview(query)
      homeLoading.value = false
      if (res) {
        let { ret, data } = res
        if (ret === 0) {
          overviewData.value = data
          // 已处置
          orderQueryStaff.value[0].value = data?.distributed || 0
          // 已审核
          orderQueryStaff.value[1].value = data?.audited || 0
          // 待处置
          orderDisposeStaff.value[0].value = data?.distributing || 0
          // 上报待审核
          orderDisposeStaff.value[1].value = data?.auditing || 0
          // 待督办反馈
          orderDisposeStaff.value[2].value = data?.feedbacking || 0
          // 已督办反馈
          orderDisposeStaff.value[3].value = data?.feedbacked || 0
        }
      }
    } catch (error) {
      console.error(error)
    }
  }

  // 获取统计总览数据-统筹版
  async function getHomeOverviewDataLeader(query) {
    try {
      const res = await getStatOrderOverview(query)
      if (res) {
        let { ret, data } = res
        if (ret === 0) {
          overviewData.value = data
          // 处理中
          orderQueryLeader.value[0].value = data?.processing || 0
          // 已超时
          orderQueryLeader.value[1].value = data?.overtime || 0
          // 重要敏感信息
          orderQueryLeader.value[2].value = data?.level3 || 0
          // 重大敏感信息
          orderQueryLeader.value[3].value = data?.level4 || 0
        }
      }
    } catch (error) {
      console.error(error)
    }
  }

  // 获取统计折线图数据
  async function getHomeStatStatByDay(query) {
    try {
      const res = await getStatStatByDay(query)
      if (res) {
        let { ret, data } = res
        if (ret === 0) {
          const xAxis = []
          const yAxis = []
          data?.map((item) => {
            xAxis.push(item.label)
            yAxis.push(item.v1)
          })
          lineData.xData = xAxis
          lineData.seriesData = yAxis
        }
      }
    } catch (error) {
      console.error(error)
    }
  }

  return {
    homeLoading,
    overviewData,
    getHomeOverviewDataStaff,
    getHomeOverviewDataLeader,
    orderQueryLeader,
    orderQueryStaff,
    orderDisposeStaff,
    lineData,
    getHomeStatStatByDay,
    getHomeTotalData,
    totalData,
    totalSum,
    typeRatioList
  }
}
